<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\api\home;

use think\Controller;
use think\Db;

/**
 * 短信控制器
 * @package app\index\controller
 */
class Sms extends Controller
{

	var $sms_config;

	public function _initialize(){
        parent::_initialize();
		set_time_limit(0);
        $this->Configobj = Db::name("admin_config");
        $ConfigData=$this->Configobj->where(['group'=>'sms','status'=>1])->order(array('id'=>'desc'))->select();
        foreach ($ConfigData as $r) {
            $this->sms_config[$r['name']] = $r['value'];
        }
    }
    public function test(){
        $ret = '{"phone":"15603760181","content":"【喜宝服饰】您的验证码是123434。如非本人操作，请忽略本短信"}';
        $statuscode=self::sendsms($ret);
        dump($statuscode);
    }
    public function sendsms($ret){
        $ret=json_decode($ret,true);
        $content=trim($ret['content']);
        $result=self::sendsms_post($content,$ret['phone']);
        $ret['inputtime']=time();
        Db::name('smslog')->insert($ret);
        $data = json_decode($result,true);
        return $data;
    }
    public function getsmstemplate($varname,$parm=array()){
        $content=Db::name('admin_config')->where(array('group'=>'sms','name'=>$varname))->value("value");
        foreach ($parm as $key => $value)
        {
            $content=str_replace("#".$key."#",$value,$content);
        }
        return $content;
    }


	public function sendsms_tpl_post($tpl_id,$tpl_value,$mobile){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, "http://yunpian.com/v1/sms/tpl_send.json");
        $encoded_tpl_value = urlencode("$tpl_value");
        $mobile = urlencode("$mobile");
        $apikey=$this->sms_config['sms_yak'];
        $post_string="apikey=$apikey&tpl_id=$tpl_id&tpl_value=#code#=1234&mobile=$mobile";
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-type: application/x-www-form-urlencoded'
        ));
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;

    }
    public function sendsms_post($content,$mobile){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, "http://yunpian.com/v1/sms/send.json");
        $content = urlencode("$content");
        $mobile = urlencode("$mobile");
        $apikey=$this->sms_config['sms_yak'];
        $post_string="apikey=$apikey&text=$content&mobile=$mobile";
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-type: application/x-www-form-urlencoded'
        ));
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;

    }
    
}